import { _decorator, instantiate, Node, Prefab, Toggle } from 'cc';
import { UIComponent } from '../../../../Framework/Scripts/Managers/UIComponent';
import { ResManager } from '../../../../Framework/Scripts/Managers/ResManager';
import { AbNames } from '../../Config/ResConfig';
import { SceneMgr } from '../../../../Framework/Scripts/Managers/SceneMgr';
const { ccclass, property } = _decorator;

/**
 * 排行
 */
@ccclass('GameRank_Ctr')
export class GameRank_Ctr extends UIComponent {
    nodeDown: Node;
    protected onLoad(): void {
        this.InitUI();
    }

    start() {
        this.InitData();
        this.setBgAni(0);
    }

    protected onDestroy(): void {
        this.unRegisterEvent();
    }

    private InitUI(){
        this.registerEvent();

        this.nodeDown = this.ViewNode('nodeDown');
        this.AddButtonListener('btnBack',this,this.OnBtnCloseClick1);

        let container = this.ViewNode('scrollViewTop/view/content');
        const toggles = container.getComponentsInChildren(Toggle);
        for (const toggle of toggles) {
            toggle.node.on('toggle', this.onToggleClick, this);
        }
    }

    private InitData(){
        this.createView(1);
    }
    
    private registerEvent(){
        // globalThis.gameMgr._eventTarget.on(EventMsgKes.loading,this.onLoadingPro,this);
    }

    private unRegisterEvent(){
        // globalThis.gameMgr._eventTarget.off(EventMsgKes.loading,this.onLoadingPro,this);
    }

    private OnBtnCloseClick1(){
        SceneMgr.inst.showHall();
    }

    private onToggleClick(toggle: Toggle) {
        let name = toggle.node.name;
        console.log('Toggle clicked:', name);

        let type = 0;
        if(name === 'Toggle1'){ //主题赛排名
            type = 1;
        }else if(name === 'Toggle2'){ //每日挑战排名
            type = 2;
        }else if(name === 'Toggle3'){  // 主题赛（文体、足球、名族）胜场次数
            type = 3;
        }else if(name === 'Toggle4'){ //登录天数
            type = 4;
        }

        this.createView(type);
    }

    private createView(type: number){
        this.nodeDown.destroyAllChildren();

        let pre = null;
        if(type == 1){
            pre = ResManager.inst.getAsset(AbNames.GameHall,'Prefabs/GameRank/GameRank1',Prefab);
        }else if(type == 2){
            pre = ResManager.inst.getAsset(AbNames.GameHall,'Prefabs/GameRank/GameRank2',Prefab);
        }else if(type == 3){
            pre = ResManager.inst.getAsset(AbNames.GameHall,'Prefabs/GameRank/GameRank3',Prefab);
        }else if(type == 4){
            pre = ResManager.inst.getAsset(AbNames.GameHall,'Prefabs/GameRank/GameRank4',Prefab);
        }

        let node = instantiate(pre);
        this.nodeDown.addChild(node);
    }
}
